package com.guchenbo.example.loadbalance;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class RoundRobinDemo {
    public static void main(String[] args) {
        List<Node> nodes = new ArrayList<>();
        nodes.add(new Node("3", 3));
        nodes.add(new Node("5", 5));
        nodes.add(new Node("2", 2));

        for (int i = 0; i < 14; i++) {
            select(nodes);
        }
    }

    public static void help(List<Node> nodes) {
        List<Integer> list = new ArrayList<>();
        nodes.forEach(n -> {
            list.add(n.curr);
        });
        System.out.println(list);
    }

    private static Node select(List<Node> nodes) {
        int total = nodes.stream().mapToInt(n -> n.weight).sum();
        help(nodes);
        nodes.forEach(n -> n.curr = n.curr + n.weight);
        // max curr
        Node select = nodes.stream().max(Comparator.comparing(n -> n.curr)).get();
        select.curr = select.curr - total;
        help(nodes);
        System.out.println(select.name);
        return select;
    }

    public static class Node {
        private String name;
        private int weight;
        private int curr;

        public Node(String name, int weight) {
            this.name = name;
            this.weight = weight;
        }

    }
}
